package cn.lg.soar.system.auth.service.impl;

import cn.lg.soar.common.util.data.DataUtil;
import cn.lg.soar.database.base.service.impl.BaseServiceImpl;
import cn.lg.soar.system.auth.entity.DataRule;
import cn.lg.soar.system.auth.entity.DataRuleItem;
import cn.lg.soar.system.auth.mapper.DataRuleItemMapper;
import cn.lg.soar.system.auth.mapper.DataRuleMapper;
import cn.lg.soar.system.auth.service.IDataRuleItemService;
import cn.lg.soar.system.config.AuthCacheKey;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.Cache;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
 * DataRuleItem服务层实现
 * 数据源
 * @author luguoxiang
 * @since 2022-04-30
 * 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
 */
public class DataRuleItemServiceImpl extends BaseServiceImpl<DataRuleItemMapper, DataRuleItem> implements IDataRuleItemService {

    @Autowired
    private DataRuleMapper dataRuleMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean saveBatch(Long id, List<DataRuleItem> items) {
        for (DataRuleItem item : items) {
            item.setRuleId(id);
        }
        remove(
                Wrappers.<DataRuleItem>lambdaQuery()
                        .eq(DataRuleItem::getRuleId, id)
        );
        return saveBatch(items);
    }

    @Override
    public List<DataRuleItem> listByRuleId(Long id) {
        return list(
                Wrappers.<DataRuleItem>lambdaQuery()
                        .eq(DataRuleItem::getRuleId, id)
        );
    }

    @Override
    public List<DataRuleItem> listByRuleId(Collection<Long> ids) {
        if (DataUtil.isEmpty(ids)) {
            return Collections.emptyList();
        }
        return list(
                Wrappers.<DataRuleItem>lambdaQuery()
                        .in(DataRuleItem::getRuleId, ids)
        );
    }

    @Override
    public boolean save(DataRuleItem entity) {
        removeResourceRuleMapCache(Collections.singletonList(entity.getId()));
        return super.save(entity);
    }

    @Override
    public boolean updateById(DataRuleItem entity) {
        removeResourceRuleMapCache(Collections.singletonList(entity.getId()));
        return super.updateById(entity);
    }

    @Override
    public boolean removeByIds(Collection<?> list) {
        removeResourceRuleMapCache(list);
        return super.removeByIds(list);
    }

    private void removeResourceRuleMapCache(Collection<?> ids) {
        if (DataUtil.isEmpty(ids)) {
            return;
        }
        List<Long> ruleIds = listObjs(
                Wrappers.<DataRuleItem>lambdaQuery()
                        .select(DataRuleItem::getRuleId)
                        .in(DataRuleItem::getId, ids),
                x -> Long.parseLong(x.toString())
        );
        if (DataUtil.isEmpty(ruleIds)) {
            return;
        }
        List<DataRule> dataRules = dataRuleMapper.selectBatchIds(ruleIds);

        Cache cache = cacheManager.getCache(AuthCacheKey.RESOURCE_RULE_MAP);
        Assert.notNull(cache, "缓存不存在");
        // 逐条清理缓存
        for (DataRule dataRule : dataRules) {
            cache.evict(dataRule.getResourceId());
        }
    }
}
